package com.ny.zmb.openapi.jh.service;

import com.alibaba.fastjson.JSONObject;
import com.mzlion.easyokhttp.HttpClient;
import com.ny.zmb.openapi.constant.JHConstant;
import com.ny.zmb.openapi.jh.JhEnum;
import com.ny.zmb.openapi.jh.UUIDUtil;
import com.ny.zmb.openapi.jh.from.CategoryVo;
import com.ny.zmb.openapi.jh.from.JHAddressFrom;

import java.util.List;

/**
 * 商品分类
 */
public class JHCategoryService {

    /**
     * 获取商品分类
     * @return
     */
    public static List<CategoryVo> category(Integer parentId){
        JSONObject jsonObject = new JSONObject();
        if (null != parentId){
            jsonObject.put("parentId",parentId);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.product.category")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result = JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONArray("data").toJavaList(CategoryVo.class);
        }
        throw new RuntimeException(result.getString("msg"));
    }
}
